import 'dart:async';
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:game_11_sleep_game11/game.dart';
import 'package:game_11_sleep_game11/main.dart';
import 'package:game_11_sleep_game11/music_player.dart';
import 'package:game_11_sleep_game11/music_player.dart';
import 'package:game_11_sleep_game11/music_player.dart';
import 'package:game_11_sleep_game11/rank.dart';
import 'package:game_11_sleep_game11/sleep.dart';

double c_r = 150;
double b_r = 30;

bool test_flag = false;

String game_name = "";
Offset center = Offset(screen_width / 2, screen_height / 2);

int music_id_01 = 0;
int music_id_02 = 0;
int music_id_03 = 0;

lead_music() async {
  music_id_01 = await MusicPlayer.lead("sounds/bg.mp3", loop: 1);
  music_id_02 = await MusicPlayer.lead("sounds/clear.mp3");
  music_id_03 = await MusicPlayer.lead("sounds/click.mp3");
}

class menu_home extends StatelessWidget {
  const menu_home({super.key});

  @override
  Widget build(BuildContext context) {
    center = Offset(screen_width / 2, screen_height / 2);
    return Scaffold(
      body: Body(),
    );
  }
}

class Body extends StatefulWidget {
  const Body({super.key});

  @override
  State<Body> createState() => _BodyState();
}

class _BodyState extends State<Body> {
  var form_key = GlobalKey<FormState>();
  late Timer move_timer;
  Sleep sleep = Sleep(Rect.fromCenter(
      center: Offset(center.dx - b_r - c_r, center.dy),
      width: b_r * 2,
      height: b_r * 2));

  void move() {
    sleep.angle += sleep.speed;
    double dx = center.dx - (c_r + b_r) * cos(sleep.angle);
    double dy = center.dy + (c_r + b_r) * sin(sleep.angle);
    sleep.rect = Rect.fromCenter(
        center: Offset(dx, dy), width: b_r * 2, height: b_r * 2);
    setState(() {});
  }

  void start_timer() {
    move_timer = Timer.periodic(Duration(milliseconds: 1000 ~/ 60), (timer) {
      move();
      setState(() {});
    });
  }

  void stop_timer() {
    move_timer.cancel();
  }

  @override
  void initState() {
    // TODO: implement initState
    lead_music();
    start_timer();
    super.initState();
  }

  @override
  void dispose() {
    // TODO: implement dispose
    stop_timer();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        Container(
          decoration: BoxDecoration(
            image: DecorationImage(
              fit: BoxFit.cover,
              image: AssetImage("images/bg.jpeg"),
            ),
          ),
        ),
        Positioned.fromRect(
          rect: sleep.rect,
          child: Container(
            decoration: BoxDecoration(
              image: DecorationImage(
                image: AssetImage("images/sleep.png"),
              ),
            ),
          ),
        ),
        Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text("羊了个羊"),
            Form(
              key: form_key,
              child: Column(
                children: [
                  Container(
                    decoration:
                        BoxDecoration(border: Border.all(color: Colors.cyan)),
                    child: TextFormField(
                      onSaved: (value) {
                        game_name = value!;
                      },
                      validator: (value) {
                        if (value!.isEmpty) {
                          return "请输入玩家姓名";
                        }
                        return null;
                      },
                      decoration: InputDecoration(
                          border: InputBorder.none, hintText: "请在此输入玩家名"),
                    ),
                  ),
                ],
              ),
            ),
            ElevatedButton(
              onPressed: () {
                if (form_key.currentState!.validate()) {
                  form_key.currentState!.save();
                  if(game_name.contains("test")){
                    test_flag = true;
                  }
                  else{
                    test_flag = false;
                  }
                  Navigator.push(context,
                      MaterialPageRoute(builder: (context) => game_home()));
                }
              },
              child: Text("开始游戏"),
            ),
            ElevatedButton(
              onPressed: () {
                Navigator.push(context,
                    MaterialPageRoute(builder: (context) => rank_home()));
              },
              child: Text("排行榜"),
            ),
          ],
        ),
      ],
    );
  }
}
